package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.YhType;
import com.ruoyi.system.domain.vo.YhDetailsVo;
import com.ruoyi.system.service.IYhTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YhDetails;
import com.ruoyi.system.service.IYhDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 烟盒信息Controller
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Api(tags = "烟盒信息管理")
@RestController
@RequestMapping("/yh/details")
public class YhDetailsController extends BaseController
{
    @Autowired
    private IYhDetailsService yhDetailsService;
    @Autowired
    private IYhTypeService yhTypeService;

    /**
     * 查询烟盒信息列表
     */
    @ApiOperation("获取烟盒信息列表")
    @PreAuthorize("@ss.hasPermi('yh:details:list')")
    @GetMapping("/list")
    public TableDataInfo yhDetailsList(YhDetails yhDetails)
    {
        startPage();
        List<YhDetailsVo> list = yhDetailsService.yhDetailsList(yhDetails);
        return getDataTable(list);
    }

    /**
     * 查询烟盒是否可以回收
     */
    @ApiOperation("查询烟盒是否可以回收")
    @PreAuthorize("@ss.hasPermi('yh:details:list')")
    @GetMapping("/checkBarCode")
    public AjaxResult checkBarCode(YhDetails yhDetails)
    {
        LambdaQueryWrapper<YhDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YhDetails::getBarCode, yhDetails.getBarCode())
                .eq(YhDetails::getState, 1);
        List<YhDetails> list = yhDetailsService.list(queryWrapper);
        if(list.size() > 0) {
            return success(list.get(0));
        }else{
            throw new ServiceException("该类型烟/条盒暂不回收！");
        }
    }

    /**
     * 查询所有烟盒类型
     */
    @ApiOperation("查询所有烟盒类型")
    @PreAuthorize("@ss.hasPermi('yh:details:list')")
    @GetMapping("/getAllType")
    public AjaxResult getAllType(YhDetails yhDetails)
    {
        YhType yhType = new YhType();
        yhType.setYhType(yhDetails.getYhType());
        List<YhType> list = yhTypeService.list(yhType.queryWrapper());
        return success(list);
    }

    /**
     * 导出烟盒信息列表
     */
    @ApiOperation("导出烟盒信息列表")
    @PreAuthorize("@ss.hasPermi('yh:details:export')")
    @Log(title = "烟盒信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhDetails yhDetails)
    {
        List<YhDetails> list = yhDetailsService.list(yhDetails.queryWrapper());
        ExcelUtil<YhDetails> util = new ExcelUtil<YhDetails>(YhDetails.class);
        util.exportExcel(response, list, "烟盒信息数据");
    }

    /**
     * 获取烟盒信息详细信息
     */
    @ApiOperation("获取烟盒信息详细")
    @ApiImplicitParam(name = "id", value = "烟盒信息ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:details:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(yhDetailsService.getById(id));
    }

    /**
     * 新增烟盒信息
     */
    @ApiOperation("新增烟盒信息")
    @PreAuthorize("@ss.hasPermi('yh:details:add')")
    @Log(title = "烟盒信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addYhDetails(@RequestBody @Valid YhDetails yhDetails)
    {
        return toAjax(yhDetailsService.addYhDetails(yhDetails));
    }

    /**
     * 修改烟盒信息
     */
    @ApiOperation("更新烟盒信息")
    @PreAuthorize("@ss.hasPermi('yh:details:edit')")
    @Log(title = "烟盒信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhDetails yhDetails)
    {
        return toAjax(yhDetailsService.updateById(yhDetails));
    }


    /**
     * 删除烟盒信息
     */
    @ApiOperation("删除烟盒信息")
    @ApiImplicitParam(name = "ids", value = "烟盒信息ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:details:remove')")
    @Log(title = "烟盒信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(yhDetailsService.removeByIds(ids));
    }
}
